<?php

require("../includes/funciones.inc.php");
include("../includes/MPDF56/mpdf.php");

function getCurrentYear()
{
	require("../includes/constants.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$strSQL = "SELECT * FROM config ORDER BY idConfig DESC";
	$rsConfig = $DB->Execute($strSQL);
	
	return $rsConfig->fields('anno');
}

function printCampeonatoGral()
{
	require("../includes/constants.php");
	
	$mpdf=new mPDF();
	$mpdf->simpleTables = true;
	$mpdf->packTableData = true;
	$mpdf->cacheTables = true;	
	$mpdf->defaultheaderfontsize = 10;	/* in pts */
	$mpdf->defaultheaderfontstyle = B;	/* blank, B, I, or BI */
	$mpdf->defaultheaderline = 1; 	/* 1 to include line below header/above footer */

	$mpdf->defaultfooterfontsize = 12;	/* in pts */
	$mpdf->defaultfooterfontstyle = B;	/* blank, B, I, or BI */
	$mpdf->defaultfooterline = 1; 	/* 1 to include line below header/above footer */
	
	$mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|http://www.campanaylima.com.ar');
	$mpdf->SetFooter('{PAGENO}');	/* defines footer for Odd and Even Pages - placed at Outer margin */
	
	$html = '<h1>CAMPEONATO GENERAL</h1>';
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
		
	//Query Equipos
	$strSQLEquipos = "SELECT u.idUser, u.userCompleteName, SUM(puntos) AS sumaPuntos FROM equipos e 
				INNER JOIN users u ON u.idUser = e.idUser 
				INNER JOIN carreras c ON e.fecha = c.fecha
				WHERE c.tipoCarrera IN ('$tipoCarreraGeneral', '$tipoCarreraUnaPta') AND LEFT(c.fecha,4) = '".getCurrentYear()."' AND c.state <> '$stateErase'
				GROUP BY u.idUser";
	
	//Query Carreras
	$strSQLCarreras = "SELECT u.idUser, userCompleteName, SUM(puntos) AS sumaPuntos FROM carreras_items ci 
				INNER JOIN users u ON u.idUser = ci.idUser 
				INNER JOIN carreras c ON ci.idCarrera = c.idCarrera
				WHERE c.state = '$statePublicado' AND c.tipoCarrera IN ('$tipoCarreraGeneral', '$tipoCarreraUnaPta') AND LEFT(c.fecha,4) = '".getCurrentYear()."' AND c.state <> '$stateErase'
				GROUP BY u.idUser";
				
	$strSQL = "SELECT @rownum := @rownum + 1 As Pos, idUser, userCompleteName, Puntos FROM (SELECT idUser, userCompleteName, SUM(sumaPuntos) AS Puntos FROM (".$strSQLEquipos." UNION ALL ".$strSQLCarreras.") General GROUP BY idUser) ConPosicion, (SELECT @rownum:=0) r ORDER BY Puntos DESC";
	$strSQL = "SELECT Pos, idUser, userCompleteName, Puntos FROM ($strSQL) campeonatoGeneral";

	$rsGeneral = $DB->Execute($strSQL);
	//Header
	$html .= '<table id="cot_table" width="100%" border="1" cellspacing="0" cellpadding="5">';
	$html .= '	<tbody>';
	$html .= '		<tr>';
	$html .= '			<th>Posici&oacute;n</th>';
	$html .= '			<th>Palomar</th>';
	$html .= '			<th>Nombre</th>';
	$html .= '			<th>Puntos</th>';
	$html .= '		</tr>';
	
	while(!$rsGeneral->EOF){
		$html .= '		<tr>';
		$html .= '			<td>'.$rsGeneral->fields('Pos').'</td>';
		$html .= '			<td>'.$rsGeneral->fields('idUser').'</td>';
		$html .= '			<td>'.$rsGeneral->fields('userCompleteName').'</td>';
		$html .= '			<td>'.$rsGeneral->fields('Puntos').'</td>';
		$html .= '		</tr>';	
		
		$rsGeneral->MoveNext();
	}
	
	$html .= '</tbody></table>';
   
	$mpdf->WriteHTML($html);  
	$mpdf->Output();	
}

function exportXLS()
{
	require("../includes/constants.php");
	require("../includes/funciones.inc.php");
	
	$arrayTituloCampos = array();
	$arrayValoresCampos = array();
	$arrayTipoDatosCampos = array();
	
	array_push($arrayTituloCampos, "Posicion");
	array_push($arrayValoresCampos, "posicion");
	array_push($arrayTipoDatosCampos, "string");
	
	array_push($arrayTituloCampos, "Palomar");
	array_push($arrayValoresCampos, "idUser");
	array_push($arrayTipoDatosCampos, "string");
	
	array_push($arrayTituloCampos, "Usuario");
	array_push($arrayValoresCampos, "userCompleteName");
	array_push($arrayTipoDatosCampos, "string");
	
	array_push($arrayTituloCampos, "Puntos");
	array_push($arrayValoresCampos, "Puntos");
	array_push($arrayTipoDatosCampos, "string");
	
	//Query Equipos
	$strSQLEquipos = "SELECT u.idUser, u.userCompleteName, SUM(puntos) AS sumaPuntos FROM equipos e 
				INNER JOIN users u ON u.idUser = e.idUser 
				INNER JOIN carreras c ON e.fecha = c.fecha
				WHERE c.tipoCarrera IN ('$tipoCarreraGeneral', '$tipoCarreraUnaPta') AND LEFT(c.fecha,4) = '".getCurrentYear()."' AND c.state <> '$stateErase'
				GROUP BY u.idUser";
	
	//Query Carreras
	$strSQLCarreras = "SELECT u.idUser, userCompleteName, SUM(puntos) AS sumaPuntos FROM carreras_items ci 
				INNER JOIN users u ON u.idUser = ci.idUser 
				INNER JOIN carreras c ON ci.idCarrera = c.idCarrera
				WHERE c.state = '$statePublicado' AND c.tipoCarrera IN ('$tipoCarreraGeneral', '$tipoCarreraUnaPta') AND LEFT(c.fecha,4) = '".getCurrentYear()."'  AND c.state <> '$stateErase' 
				GROUP BY u.idUser";
				
	$strSQL = "SELECT @rownum := @rownum + 1 As posicion, idUser, userCompleteName, Puntos FROM (SELECT idUser, userCompleteName, SUM(sumaPuntos) AS Puntos FROM (".$strSQLEquipos." UNION ALL ".$strSQLCarreras.") General GROUP BY idUser) ConPosicion, (SELECT @rownum:=0) r ORDER BY Puntos DESC";
	$strSQL = "SELECT posicion, idUser, userCompleteName, Puntos FROM ($strSQL) campeonatoGeneral";
	
	exportarExcel($arrayTituloCampos, $arrayValoresCampos, $arrayTipoDatosCampos,  'General', $strSQL);
}

switch($_REQUEST['accion'])
{
	case "print":
		printCampeonatoGral();
		break;
	case "exportXLS":
		exportXLS();
		break;
}
?>